/**
 * Created by Allen on 2018/2/27.
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from "share/errorMessage";

export default {
  //开通发票的值列表
  invoiceTurnonList(params) {
    return httpFetch.get(`${config.baseUrl}/api/invoice/header/list/all?${params}`);
  },

  //定时提醒的数据列表
  invoiceSchedule(companyOID, page, pageSize) {
    return httpFetch.get(`${config.baseUrl}/api/taskConfig/queryByCompanyOIDAndCode?code=InvoiceScheduledNoticeTask&companyOID=${companyOID}&page=${page}&size=${pageSize}`);
  },

  //保存微信电子发票
  saveWechatInvoice(params) {
    return httpFetch.post(`${config.baseUrl}/api/function/profiles`, params);
  },

  //获取公司信息
  getCompanyInfo() {
    return httpFetch.get(`${config.baseUrl}/api/function/profiles/company`);
  },

  //获取租户和公司profile信息
  getProfileInfo(tenantMode) {
    if (tenantMode) {
      return httpFetch.get(`${config.baseUrl}/api/function/profiles?roleType=TENANT`)
        .catch(e => {
          errorMessage(e.response);
          throw e;
        });
    } else {
      return httpFetch.get(`${config.baseUrl}/api/function/profiles/company`)
        .catch(e => {
          errorMessage(e.response);
          throw e;
        });
    }
  },

  //保存profile信息
  saveProfile(params, tenantMode) {
    return httpFetch.post(`${config.baseUrl}/api/function/profiles/setProfile`, params)
      .catch(e => {
        errorMessage(e.message);
        throw e;
      });
  },
  
  //添加定时提醒
  addSchedule(params) {
    return httpFetch.post(`${config.baseUrl}/api/taskConfig`, params);
  },

  //删除提醒事项
  deleteItem(id) {
    return httpFetch.delete(`${config.baseUrl}/api/taskConfig`, id);
  },

  //发票超时管控
  handleOverTime(params) {
    return httpFetch.post(`${config.baseUrl}/api/invoice/overtime`, params);
  },

  //是否存在可查验的抬头
  getAllTitleList() {
    return httpFetch.get(`${config.baseUrl}/api/invoice/header/list`);
  },

  //获取开通的发票配置
  getInvoiceConfig(companyOID) {
    return httpFetch.get(`${config.baseUrl}/api/invoice/overtime/${companyOID}`);
  },

  /**
   * @param params
   * @desc 获取法人实体关联订单list
   */
  getLegalEntities(params) {
    return httpFetch.get(`${config.baseUrl}/api/legalEntity/with/order/page`, params);
  },
  /**
   * @param params
   * @desc 开通指定法人对应服务 || 禁用指定法人对应服务
   */
  designatedLegalEntities(params) {
    let url = '/api/legalEntity/with/order',
      method;
    params.id ? method = httpFetch['delete'] : method = httpFetch['post'];
    return method(`${config.baseUrl}${url}`, params);
  },
  /**
   * @desc 获取FP
   */
  getFunctionProfile() {
    return httpFetch.get(`${config.baseUrl}/api/function/profiles`);
  },
  /**
   * @desc 获取发票过期提醒List
   */
  getExpirationReminders(companyOID) {
    return httpFetch.get(`/api/invoice/overtime/${companyOID}`)
      .catch(e => {
        errorMessage(e.response);
      });
  },
  /**
   * @desc 提交发票过期提醒
   * @params
   */
  submitExpirationReminder(values, requestBody, companyOID) {
    const { sequence, enabled, description, receiptTypeList, taxCodeList, expiration, effectiveTime, forceEnabled, expiryRemindDay } = values;
    const { staticCalibrationFlag, expiryDay, dynamicRuleFailureTime } = expiration;
    const dynamicRuleFailureArray = !staticCalibrationFlag && dynamicRuleFailureTime.split('-');
    const effectiveArray = effectiveTime.split('-');
    const effectiveMonth = effectiveArray[0];
    const effectiveDay = effectiveArray[1];
    let dynamicRuleFailureMonth, dynamicRuleFailureDay;
    let companyOid = companyOID;
    if(dynamicRuleFailureArray){
      dynamicRuleFailureMonth = (dynamicRuleFailureArray[0]);
      dynamicRuleFailureDay =  (dynamicRuleFailureArray[1]);
    }
    requestBody = {
      ...requestBody,
      companyOid,
      sequence,
      enabled,
      description,
      receiptTypeList,
      taxCodeList,
      forceEnabled,
      staticCalibrationFlag,
      dynamicRuleFailureMonth,
      dynamicRuleFailureDay,
      effectiveMonth,
      effectiveDay,
      expiryDay,
      dynamicRuleFailureTime,
      effectiveTime,
      expiryRemindDay
    };
    return new Promise((resolve, reject) => {
      httpFetch.post(`/api/invoice/overtime`, requestBody)
        .then(() => {
          resolve(true);
        })
        .catch(e => {
          errorMessage(e.response);
          reject(false);
        });
    });
  },
  /**
   * @desc 删除发票过期提醒
   * @params
   */
  deleteExpirationReminder(id) {
    return httpFetch.delete(`/api/invoice/overtime?id=${id}`)
      .catch(e => {
        errorMessage(e.response);
      });
  },
  /**
   * @desc 批量开通指定法人对应服务
   * @params
   */
  batchOpenLegalEntity(params){
    return httpFetch.post(`/api/legalEntity/with/order/open/batch?serviceTypeCode=${params.serviceTypeCode}&all=${params.all}`, params.arr)
      .catch(e => {
        errorMessage(e.response)
    })
  },

  /**
   * @desc 批量关闭指定法人对应服务
   * @params
   */
  batchCloseLegalEntity(params) {
    return httpFetch.post(`/api/legalEntity/with/order/close/batch?serviceTypeCode=${params.serviceTypeCode}&all=${params.all}`, params.arr)
      .catch(e => {
        errorMessage(e.response)
      })
  }
};
